import string
import tkinter as tk
import random
from datetime import datetime
from logger_config import logger




class CreateNumber:
    vins = []
    plates = []
    batteries = []
    phones = []
    IDnumbers = []
    credit_codes = []
    battery_nameplates_z = []
    battery_nameplates_q = []

    def __init__(self):
        pass

    # 生成车辆VIN码
    def creat_vin(self, entry_vin, text_vin):
        try:
            vin_start = 'LFWADRJF0110'
            numbers = [str(random.randint(0, 9)) for _ in range(5)]
            vin_end = ''.join(numbers)
            vin = vin_start + vin_end
            logger.info("Generated VIN number: %s",vin)
            if vin not in self.vins:
                self.vins.append(vin)
            entry_vin.delete(0, "end")
            entry_vin.insert("end", vin)
            text_vin.insert(tk.END, vin + '\n')
            return vin
        except Exception as e:
            logger.error("Error generating VIN number: %s",e)
    # 生成车牌号
    def creat_plate(self, entry_plate, text_plate):
        try:
            plate_start = '浙'
            chars = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
                     'W', 'X', 'Y', 'Z']
            plate_mid = random.choice(chars)
            plate_end = random.choices(str(string.digits) + ''.join(chars), k=5)
            plate = plate_start + plate_mid + ''.join(plate_end)
            logger.info("Generated plate number: %s",plate)
            if plate not in self.plates:
                self.plates.append(plate)
            entry_plate.delete(0, "end")
            entry_plate.insert("end", plate)
            text_plate.insert(tk.END, plate + '\n')
            return plate
        except Exception as e:
            logger.error("Error generating plate number: %s",e)
    # 生成电池编号
    def creat_battery(self, entry_battery, text_battery):
        try:
            batteryNo = str(random.randint(10 ** 11, 10 ** 12 - 1))
            if batteryNo not in self.batteries:
                self.batteries.append(batteryNo)
            logger.info("Generated battery number: %s",batteryNo)
            entry_battery.delete(0, "end")
            entry_battery.insert("end", batteryNo)
            text_battery.insert(tk.END, batteryNo + '\n')
            return batteryNo
        except Exception as e:
            logger.error("Error generating battery number: %s",e)
    # 生成手机号码
    def creat_phone(self,entry_phone, text_phone):
        try:
            phone_start = random.choice(['135', '136', '137', '138', '139', '147', '150', '151', '152', '157', '158', '159', '178', '182', '183', '184'])
            phone_end = ''.join(random.choices(str(string.digits),k=8))
            phone = phone_start + phone_end
            logger.info("Generated phone: %s",phone)
            if phone not in self.phones:
                self.phones.append(phone)
            entry_phone.delete(0, "end")
            entry_phone.insert("end", phone)
            text_phone.insert(tk.END, phone + '\n')
            return phone
        except Exception as e:
            logger.error("Error generating phone number: %s",e)

    # 生成身份证号
    def creat_IDnumber(self,entry_IDnumber, text_IDnumber):
        try:
            region_code = str(random.randint(110000, 659004))
            year = str(random.randint(1949, 2022))
            month = str(random.randint(1, 12)).rjust(2, '0')
            day = str(random.randint(1, 28)).rjust(2, '0')
            order = str(random.randint(1, 999)).rjust(3, '0')
            idnumber = region_code + year + month + day + order
            factor_list = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
            check_code_list = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']
            sum = 0
            for i in range(17):
                sum += int(idnumber[i]) * factor_list[i]
            sum %= 11
            check_code = check_code_list[sum]
            IDnumber = idnumber + str(check_code)
            logger.info("Generated IDnumber: %s",IDnumber)
            if IDnumber not in self.IDnumbers:
                self.IDnumbers.append(IDnumber)
            entry_IDnumber.delete(0, "end")
            entry_IDnumber.insert("end", IDnumber)
            text_IDnumber.insert(tk.END, IDnumber + '\n')
            return IDnumber
        except Exception as e:
            logger.error("Error occurred while generating IDnumber: %s",e)

    # 生成统一社会信用代码
    def creat_credit_code(self,entry_credit_code, text_credit_code):
        try:
            # 不带校验规则
            # 随机生成组织机构代码（9位数字）
            org_code = ''.join(random.choices('0123456789', k=9))
            # 随机生成登记管理机关行政区划码
            admin_code = str(random.randint(11, 82)).zfill(2) + str(random.randint(1, 99)).zfill(2) + str(
                random.randint(1, 99)).zfill(2) + str(random.randint(1, 99)).zfill(2)
            # 随机生成校验码（1位数字或大写字母）
            validate_code = random.choice('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ')
            # 组合生成统一社会信用代码
            credit_code = admin_code + org_code + validate_code
            logger.info("Generated credit code: %s",credit_code)
            if credit_code not in self.credit_codes:
                self.credit_codes.append(credit_code)
            entry_credit_code.delete(0, "end")
            entry_credit_code.insert("end", credit_code)
            text_credit_code.insert(tk.END, credit_code + '\n')
            return credit_code
        except Exception as e:
            logger.error("Error occurred while generating credit code: %s",e)

    #  生成重卡玖行电池铭牌
    def creat_battery_nameplate_z(self,entry_battery_nameplate,text_battery_nameplate):
        try:
            current_time = datetime.now().strftime('%Y%m%d')
            count = random.randint(1, 999)
            battery_nameplate_z = '02HD{}{:03d}'.format(current_time,count)
            # 记录生成的电池铭牌到日志中
            logger.info("Generated battery nameplate: %s",battery_nameplate_z)
            if battery_nameplate_z not in self.battery_nameplates_z:
                self.battery_nameplates_z.append(battery_nameplate_z)
            entry_battery_nameplate.delete(0, "end")
            entry_battery_nameplate.insert("end", battery_nameplate_z)
            text_battery_nameplate.insert(tk.END, battery_nameplate_z + '\n')
            return battery_nameplate_z
            # return battery_nameplate
        except Exception as e:
            logger.error("Error occurred while generating battery nameplate: %s",e)
    # 生成轻卡电池铭牌
    def creat_battery_nameplate_q(self,entry_battery_nameplate,text_battery_nameplate):
        try:
            batt_start = 'ODAPB0010001BCD9F0000'
            batt_end = random.randint(1,999)
            battery_nameplate_q = batt_start + str(batt_end).zfill(3)
            logger.info("Generated battery nameplate: %s", battery_nameplate_q)
            if battery_nameplate_q not in self.battery_nameplates_q:
                self.battery_nameplates_q.append(battery_nameplate_q)
            entry_battery_nameplate.delete(0, "end")
            entry_battery_nameplate.insert("end", battery_nameplate_q)
            text_battery_nameplate.insert(tk.END, battery_nameplate_q + '\n')
            return battery_nameplate_q
        except Exception as e:
            logger.error("Error occurred while generating battery nameplate: %s",e)


